<?php
/**
 * Created by JetBrains PhpStorm.
 * User: akrug
 * Date: 01.06.12
 * Time: 12:02
 * To change this template use File | Settings | File Templates.
 */

abstract class UcsMvc_AbstractForm extends Zend_Form
{

    /**
     * @var
     */
    protected $_language;

    /**
    * @var Model_UserAcl
    */
    protected $_acl = null;

    /**
     * @var null
     */
    protected $_request = null;

    /**
     * @var Zend_Auth
     */
    protected $_identity = null;

    public function __construct(){
        //bezieht angemeldeten Benutzer
        $this->_identity = $this->readIdentity();

        $fcontr = Zend_Controller_Front::getInstance();
        $this->_request = $fcontr->getRequest();

        //ACL
        $this->_acl = new Model_UserAcl();

        //sets current language from Locale
        $this->_language = Zend_Registry::get('Zend_Locale')->getLanguage();

        //retrieves Translator from bootstrap
        $this->setTranslator($this->getDefaultTranslator());

        $this->setMethod('post');
        //standard pfad
        $this->setAction('');

        $this->prepareAttribs();

        parent::__construct();

        $this->getDecorator('HtmlTag')->setOption('tag', 'div');
    }

    /**
     * zeigt auf sich selbst
     */
    protected function createAction(){
        $module     = strtolower($this->_request->getModuleName());
        $controller = strtolower($this->_request->getControllerName());
        $action     = strtolower($this->_request->getActionName());

        $baseUrl = $this->getView()->BaseUrl();

        $this->setAction($baseUrl.'/'.$module.'/'.$controller.'/'.$action);
    }

    protected function prepareDecorators(){
        foreach($this->getElements() as $e){
            $d = $e->getDecorator('Label');

            if($d){
                $d->setTag('div')->removeOption('tag');

                $class = 'label-'.preg_replace('/^Zend_Form_Element_/', '', $e->getType());
                $d->setTagClass($class);
            }

            $d = $e->getDecorator('HtmlTag');

            if($d){
                $d->setTag('div')->removeOption('tag');
                $class = 'type-'.preg_replace('/^Zend_Form_Element_/', '', $e->getType());
                $d->setOption('class', $class);
            }
        }
    }

    /**
     * gibt dem formular eine id in der Form Unterverzeichnis_Dateiname mit
     */
    public function prepareAttribs(){
        $this->setAttrib('id', strtolower(
            preg_replace('/^'.ucfirst(
                strtolower(
                    $this->_request->getModuleName()
                )
            ).'_Form_/', '',get_class($this))
        ));
    }

    /**
     * ueberprueft, ob benutzerrechte auf in der acl eingetragenes element vorhanden sind
     *
     * @param $privilege
     *
     * @return bool
     */
    protected function accesAllowed($privilege){
        $module     = strtolower($this->_request->getModuleName());
        $controller = strtolower($this->_request->getControllerName());
        $resource   = strtolower($module.':'.$controller);

        if($this->_acl->isAllowed($this->_identity->role, $resource, $privilege)){
            return true;
        }else{
            return false;
        }

    }


    /*
     * liest die Benutzerdaten des angemeledeten Benutzers aus
     */
    protected function readIdentity(){
        if(Zend_Auth::getInstance()->hasIdentity()){
            return Zend_Auth::getInstance()->getIdentity();
        }else{
            return false;
        }
    }
}
